home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1911 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  53 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Needed: Friend function example.
  5. Date: 14 Jan 1996 02:27:20 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4d9pm8$shr@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe3.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 13, 1996 21:55:56 in article <Needed: Friend function example.>,
  15. 'Bob Wade <bwade@nando.net>' wrote: 
  16.  
  17.  
  18. >Could someone please provide an example of a friend function which does  
  19. >not involve istream or ostream, and does not involve a global function? 
  20.  
  21. What do you mean by "global"?  Member of another class? 
  22.  
  23. >I am having trouble declaring a friend function which compiles.  Making  
  24. >the class a friend works fine.  Every source I have found has the same  
  25. >example. I know this stuff is supposed to be reusable but this is  
  26. >ridiculous! ;) 
  27.  
  28. Maybe you should post a small sample code that demonstrates 
  29. what you're trying to do.  In the meanwhile,  here's a sample that 
  30. meets your specs except for the global function.  Hope it 
  31. helps some. 
  32.  
  33. class Foo 
  34.  { 
  35.    friend int ff ( Foo *); 
  36.    public: 
  37.       Foo(int i) : val(i) {}; 
  38.    private: 
  39.       int val; 
  40.  }; 
  41.  
  42.  
  43. int ff (Foo * f) 
  44.  { 
  45.    return f->val++; 
  46.  } 
  47.  
  48.  
  49. -- 
  50. Pete Grant 
  51. Kalevi, Inc. 
  52. Object Oriented Software Design & Development
  53.